<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/product.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a href="<?php echo $insert; ?>" class="button"><?php echo $button_insert; ?></a><a onclick="$('#form').attr('action', '<?php echo $copy; ?>'); $('#form').submit();" class="button"><?php echo $button_copy; ?></a><a onclick="$('form').submit();" class="button"><?php echo $button_delete; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
        <table class="list">
          <thead>
            <tr>
              <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
              <td class="center"><?php echo $column_image; ?></td>
              <td class="left"><?php if ($sort == 'pd.name') { ?>
                <a href="<?php echo $sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_name; ?>"><?php echo $column_name; ?></a>
                <?php } ?></td>
              <td class="left"><?php if ($sort == 'p.group') { ?>
                <a href="<?php echo $sort_group; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_group; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_group; ?>"><?php echo $column_group; ?></a>
                <?php } ?></td>
              <td class="left"><?php if ($sort == 'p.purchase_code') { ?>
                <a href="<?php echo $sort_purchase_code; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_purchase_code; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_purchase_code; ?>"><?php echo $column_purchase_code; ?></a>
                <?php } ?></td>
              <td class="left">
                <?php echo $column_purchase_price; ?>
              </td>
              <td class="left"><?php if ($sort == 'p.sale_code') { ?>
                <a href="<?php echo $sort_sale_code; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_sale_code; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_sale_code; ?>"><?php echo $column_sale_code; ?></a>
                <?php } ?></td>
              <td class="left">
                <?php echo $column_sale_price; ?>
              </td>
              <td class="right"><?php if ($sort == 'p.quantity') { ?>
                <a href="<?php echo $sort_quantity; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_quantity; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_quantity; ?>"><?php echo $column_quantity; ?></a>
                <?php } ?></td>
              <td class="left"><?php if ($sort == 'p.status') { ?>
                <a href="<?php echo $sort_status; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_status; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_status; ?>"><?php echo $column_status; ?></a>
                <?php } ?></td>
              <td class="right"><?php echo $column_action; ?></td>
            </tr>
          </thead>
          <tbody>
            <tr class="filter">
              <td></td>
              <td></td>
              <td><input type="text" name="filter_name" value="<?php echo $filter_name; ?>" /></td>
              <td>
                  <select name="filter_group">
                      <option value="*"></option>
                      <?php foreach($groups as $group){ ?>                      
                      <?php if($group['group_id'] == $filter_group){ ?>
                      <option value="<?php echo $group['group_id']; ?>" selected="selected"><?php echo $group['name']; ?></option>
                      <?php } else { ?>
                      <option value="<?php echo $group['group_id']; ?>"><?php echo $group['name']; ?></option>
                      <?php } ?>
                      <?php } ?>
                  </select>
              </td>
              <td><input type="text" name="filter_purchase_code" value="<?php echo $filter_purchase_code; ?>" /></td>
              <td>
                  <select name="filter_supplier">
                      <option value="0"></option>
                      <?php foreach($suppliers as $supplier){ ?>
                      <?php if($supplier['supplier_id'] == $filter_supplier){ ?>
                      <option value="<?php echo $supplier['supplier_id']; ?>" selected="selected"><?php echo $supplier['name']; ?></option>
                      <?php }else{ ?>
                      <option value="<?php echo $supplier['supplier_id']; ?>"><?php echo $supplier['name']; ?></option>
                      <?php } ?>
                      <?php } ?>
                  </select>
              </td>
              <td><input type="text" name="filter_sale_code" value="<?php echo $filter_sale_code; ?>" /></td>
              <td><input type="text" name="filter_sale_price" value="<?php echo $filter_sale_price; ?>" /></td>
              <td align="right"><input type="text" name="filter_quantity" value="<?php echo $filter_quantity; ?>" style="text-align: right;" /></td>
              <td><select name="filter_status">
                  <option value="*"></option>
                  <?php if ($filter_status) { ?>
                  <option value="1" selected="selected"><?php echo $text_enabled; ?></option>
                  <?php } else { ?>
                  <option value="1"><?php echo $text_enabled; ?></option>
                  <?php } ?>
                  <?php if (!is_null($filter_status) && !$filter_status) { ?>
                  <option value="0" selected="selected"><?php echo $text_disabled; ?></option>
                  <?php } else { ?>
                  <option value="0"><?php echo $text_disabled; ?></option>
                  <?php } ?>
                </select></td>
              <td align="right"><a onclick="filter();" class="button"><?php echo $button_filter; ?></a></td>
            </tr>
            <?php if ($items) { ?>
            <?php foreach ($items as $item) { ?>
            <tr>
              <td style="text-align: center;"><?php if ($item['selected']) { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $item['item_id']; ?>" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $item['item_id']; ?>" />
                <?php } ?></td>
              <td class="center"><img src="<?php echo $item['image']; ?>" alt="<?php echo $item['name']; ?>" style="padding: 1px; border: 1px solid #DDDDDD;" /></td>
              <td class="left"><?php echo $item['name']; ?></td>
              <td class="left">
                  <?php if($item['groups']){ ?>
                    <?php foreach($item['groups'] as $group){ ?>
                    <?php echo $group['name']; ?> 
                    <?php } ?>
                  <?php } ?>
              </td>
              <td class="left"><?php echo $item['purchase_code']; ?></td>
              <td class="left">
                  <?php if($item['purchases']){ ?>
                    <?php foreach($item['purchases'] as $purchase){ ?>
                        <?php foreach($item['suppliers'] as $supplier){ ?>
                        <?php if($purchase['supplier_id'] == $supplier['supplier_id']){ ?>
                        <span class="left"><?php echo $supplier['short_name']; ?>:</span> <span class="right"><?php echo $purchase['price']; ?></span><br />
                        <?php } ?>
                        <?php } ?>
                    <?php } ?>
                  <?php }else{ ?>
                  <?php echo $text_undefined; ?>
                  <?php } ?>
              </td>
              <td class="left"><?php echo $item['sale_code']; ?></td>
              <td class="left">
                  <?php if($item['sale_prices']){ ?>
                  <?php foreach($item['sale_prices'] as $sale_price){ ?>
                    <?php foreach($item['sale_types'] as $sale_type){ ?>
                        <?php if($sale_type['type_id'] == $sale_price['type_id']){ ?>
                            <?php echo $sale_type['type_name']; ?>: <?php echo $sale_price['price']; ?><br/>
                        <?php } ?>
                    <?php } ?>
                  <?php } ?>
                  <?php }else{ ?>
                  <?php echo $text_undefined; ?>
                  <?php } ?>
              </td>
              <td class="right"><?php echo $item['quantity']; ?></td>
              <td class="left"><?php echo $item['status']; ?></td>
              <td class="right"><?php foreach ($item['action'] as $action) { ?>
                [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
                <?php } ?></td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="center" colspan="8"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </form>
      <div class="pagination"><?php echo $pagination; ?></div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
function filter() {
	url = 'index.php?route=item/item&token=<?php echo $token; ?>';
	
	var filter_name = $('input[name=\'filter_name\']').attr('value');
	
	if (filter_name) {
		url += '&filter_name=' + encodeURIComponent(filter_name);
	}
	
	var filter_purchase_code = $('input[name=\'filter_purchase_code\']').attr('value');
	
	if (filter_purchase_code) {
		url += '&filter_purchase_code=' + encodeURIComponent(filter_purchase_code);
	}
	
        var filter_supplier = $('select[name=\'filter_supplier\']').attr('value');
	
	if (filter_supplier != 0) {
		url += '&filter_supplier=' + encodeURIComponent(filter_supplier);
	}
        
        var filter_group = $('select[name=\'filter_group\']').attr('value');
	
	if (filter_group != '*') {
		url += '&filter_group=' + encodeURIComponent(filter_group);
	}
	
	var filter_sale_code = $('input[name=\'filter_sale_code\']').attr('value');
	
	if (filter_sale_code) {
		url += '&filter_sale_code=' + encodeURIComponent(filter_sale_code);
	}
	
	var filter_quantity = $('input[name=\'filter_quantity\']').attr('value');
	
	if (filter_quantity) {
		url += '&filter_quantity=' + encodeURIComponent(filter_quantity);
	}
	
	var filter_status = $('select[name=\'filter_status\']').attr('value');
	
	if (filter_status != '*') {
		url += '&filter_status=' + encodeURIComponent(filter_status);
	}	

	location = url;
}
//--></script> 
<script type="text/javascript"><!--
$('#form input').keydown(function(e) {
	if (e.keyCode == 13) {
		filter();
	}
});
//--></script> 
<script type="text/javascript"><!--
$('input[name=\'filter_name\']').autocomplete({
	delay: 500,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {		
				response($.map(json, function(item) {
					return {
						label: item.name,
						value: item.item_id
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'filter_name\']').val(ui.item.label);
						
		return false;
	},
	focus: function(event, ui) {
      	return false;
   	}
});

$('input[name=\'filter_purchase_code\']').autocomplete({
	delay: 500,
	source: function(request, response) {
		$.ajax({
			url: 'index.php?route=catalog/product/autocomplete&token=<?php echo $token; ?>&filter_purchase_code=' +  encodeURIComponent(request.term),
			dataType: 'json',
			success: function(json) {		
				response($.map(json, function(item) {
					return {
						label: item.model,
						value: item.item_id
					}
				}));
			}
		});
	}, 
	select: function(event, ui) {
		$('input[name=\'filter_purchase_code\']').val(ui.item.label);
						
		return false;
	},
	focus: function(event, ui) {
      	return false;
   	}
});
//--></script> 
<?php echo $footer; ?>